package com.xywl.escort.view.order.submit;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.DefaultResp;
import com.xywl.escort.bean.SubmitOrderReq;
import com.xywl.escort.bean.SubmitOrderResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class SubmitOrderPresenter implements SubmitOrderContract.Presenter {
    private SubmitOrderContract.View mView;
    private ApiServer mApiServer;

    @Inject
    SubmitOrderPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(SubmitOrderContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadAddrDefault() {
        BaseReq req = new BaseReq();
        mApiServer.addrDefault(req)
                .compose(RxUtil.<DefaultResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<DefaultResp>bindToLife())
                .subscribe(new BaseSubscriber<DefaultResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadAddrFailure();
                    }

                    @Override
                    public void onNext(DefaultResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.loadAddrSuccess(resp);
                        } else {
                            mView.loadAddrFailure();
                        }
                    }
                });
    }

    @Override
    public void submitOrder(SubmitOrderReq req) {
        mApiServer.submitOrder(req)
                .compose(RxUtil.<SubmitOrderResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<SubmitOrderResp>bindToLife())
                .subscribe(new BaseSubscriber<SubmitOrderResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.submitOrderFailure("提交订单失败");
                    }

                    @Override
                    public void onNext(SubmitOrderResp resp) {
                        if (null != resp && null != resp.data) {
                            if (resp.isOk()) {
                                mView.submitOrderSuccess(resp.data);
                            } else {
                                mView.submitOrderFailure(resp.msg);
                            }
                        } else {
                            mView.submitOrderFailure("提交订单失败");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("SubmitOrderPresenter").e("unSubscribe");
        mView = null;
    }
}
